﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using WiseLink.RDS.Resource;
namespace CCF20Model.FaultHandler
{
    public class FindVehicleByFaultHandler2 : FaultHandlerBase
    {
        public FindVehicleByFaultHandler2(Fault fault, CCF20Entities entities)
        {
            this.MyFault = fault;
            this.ccf20Ent = entities;
            this.PreviousHandler = null;
            LogStart("v2");
        }
        public override void ProcessRequest()
        {
            LoggerHelper.LogZICDebug(MyFault, null);
            IFaultHandler nextHandler = null;
            
            MyDevice = ccf20Ent.Devices.Where(d => d.IDCSimNumber == this.MyFault.IDCSimNumber && d.IsRegistered == true).FirstOrDefault();

            if (MyDevice == null || MyDevice.Vehicle == null)
            {
                CloseFault("未找到车辆");
                LoggerHelper.LogZICDebug(string.Format("[查找车辆]没有找到IDCSIM卡号为{0},并且已经注册成功的车辆", MyFault.IDCSimNumber));
                nextHandler = new CompleteFaultHandler(this);
            }
            else
            {
                MyVehicle = MyDevice.Vehicle;
                if (MyDevice.IsDefected == true)
                {
                    //如果已经确认设备损坏，则设备损坏表里的记录就没有了。
                    LoggerHelper.LogZICDebug(string.Format("[取消设备表里的损坏标识]设备SIM卡号为{0},原先标注为损坏，现在因为收到心跳，从新标注为正常", MyFault.IDCSimNumber));
                    MyDevice.IsDefected = false;

                }
                else
                {
                    //如果还没有确认设备损坏，设备损坏表里的记录就要清除。
                    TroubledDevice troubleDevice = MyDevice.TroubledDevices.FirstOrDefault();
                    if (troubleDevice != null)
                    {
                        LoggerHelper.LogZICDebug(string.Format("[删除损坏设备里的记录]设备SIM卡号为{0}", troubleDevice.Device.IDCSimNumber));
                        ccf20Ent.TroubledDevices.DeleteObject(troubleDevice);
                    }
                    else
                    {
                        LoggerHelper.LogZICDebug(string.Format("[设备正常,损坏设备里也没有该设备的记录]"));
                    }
                }
                
                
                
                LoggerHelper.LogZICDebug(string.Format("[查找车辆]找到SIM卡号为{0},并且已经注册成功的车辆", MyFault.IDCSimNumber));
                InitializeVehicle();
                LoggerHelper.LogZICDebug(string.Format("[查找车辆]车辆的上次联系结果为{0}", MyVehicle.LastContactResult));
                if (string.IsNullOrEmpty(MyVehicle.LastContactResult) || string.IsNullOrWhiteSpace(MyVehicle.LastContactResult) ||
                                        MyVehicle.LastContactResult.Contains(WiseLinkRDSStatus.Contact_NotContactYet))
                {
                    
                    nextHandler = new ProcessMaintenaceHandler2(this);
                    
                }
                else if (MyVehicle.LastContactResult.Contains(WiseLinkRDSStatus.Contact_Reject))
                {
                    nextHandler = new RejectCustomerFaultHandler2(this);

                }
                else if (MyVehicle.LastContactResult.Contains(WiseLinkRDSStatus.Contact_Giveup))
                {
                    nextHandler = new GiveUpCustomerFaultHandler2(this);

                }
                else if ( MyVehicle.LastContactResult.Contains(WiseLinkRDSStatus.Contact_WillRepair))
                    
                {
                    nextHandler = new WillRepairCustomerFaultHandler2(this);
                }
                else
                {
                    LoggerHelper.LogZICDebug(string.Format("[查找车辆]无法判定车辆上次联系状态:{0}", MyVehicle.LastContactResult));
                    CloseFault("无法判定车辆上次联系状态");
                    nextHandler = new CompleteFaultHandler(this);
                }


            }
            nextHandler.ProcessRequest();
            
            
        }

        private void InitializeVehicle()
        {
            MyFault.VehicleID = MyVehicle.VehicleID;
            if (MyFault.CurrentMileage != null && MyFault.CurrentMileage > 0)
            {
                //对于一汽大众的车子有里程
                MyVehicle.CurrentMileage = MyFault.CurrentMileage;
                MyVehicle.LatestActiveTime = DateTime.Now;

                LoggerHelper.LogZICDebug(string.Format("[车辆初始化] Vehicle ={0} IDC={1} 当前里程被更新为{2}",
                                                            MyVehicle.VehicleID,
                                                            MyVehicle.Devices.First().IDCSimNumber,
                                                            MyVehicle.CurrentMileage));

                //如果该车辆的上次服务里程和时间为空，则用当前里程和时间初始化,(包括车辆的维护项目）
                if (MyVehicle.LastServiceMileage == null || MyVehicle.LastServcieDate == null)
                {
                    MyVehicle.LastServiceMileage = MyFault.CurrentMileage;
                    MyVehicle.LastServcieDate = DateTime.Now;
                    LoggerHelper.LogZICDebug(string.Format(@"[车辆初始化]车牌为:{0}的车辆的上次服务里程和时间还未初始化,以收到的故障码:{1} 的当前里程:{2}和当前时间{3}初始化",
                                                                MyVehicle.VehicleLicense,
                                                                MyFault.Code,
                                                                MyFault.CurrentMileage,
                                                                DateTime.Now.ToLongDateString()));
                }
                foreach (VehicleMaintenanceRecord vmr in MyVehicle.VehicleMaintenanceRecords.Where(m => m.IsActive))
                {
                    
                    if (vmr.LastServiceMileage == null || vmr.LastServiceMileage == 0 || vmr.LastServiceDate == null)
                    {
                        vmr.LastServiceMileage = MyFault.CurrentMileage;
                        vmr.LastServiceDate = DateTime.Now;
                        LoggerHelper.LogZICDebug(string.Format(@"[车辆初始化]初始化保养记录:{0} 次数:{1} 上次服务时间和上次服务里程为0或空，以收到的故障码:{2} 的当前里程:{3}和当前时间{4} 初始化",
                                                                        vmr.MaintenanceType.Name,
                                                                        vmr.Priority,
                                                                        MyFault.Code,
                                                                        MyFault.CurrentMileage,
                                                                        DateTime.Now.ToLongDateString()));
                    }
                    else
                    {
                        LoggerHelper.LogZICDebug(string.Format(@"[车辆初始化]已有合法的保养记录:{0}，次数:{1} 上次服务时间:{2} 上次服务里程:{3}",
                                                                        vmr.MaintenanceType.Name,
                                                                        vmr.Priority,
                                                                        vmr.LastServiceDate,
                                                                        vmr.LastServiceMileage));
                    }
                }

            }
            else
            {
                if (MyVehicle.LastServcieDate == null)
                {
                    MyVehicle.LastServcieDate = DateTime.Now;
                    LoggerHelper.LogZICDebug(string.Format(@"[非一汽大众车辆初始化]车牌为:{0}的车辆的上次服务时间,以收到的故障码:{1} 当前时间{2}初始化",
                                                                MyVehicle.VehicleLicense,
                                                                MyFault.Code,
                                                                DateTime.Now.ToLongDateString()));
                }
                foreach (VehicleMaintenanceRecord vmr in MyVehicle.VehicleMaintenanceRecords.Where(m => m.IsActive))
                {

                    if (vmr.LastServiceDate == null)
                    {
                        vmr.LastServiceDate = DateTime.Now;
                        LoggerHelper.LogZICDebug(string.Format(@"[非一汽大众车辆初始化]初始化保养记录:{0} 上次服务时间 以收到的故障码:{1}和当前时间{2} 初始化",
                                                                        vmr.MaintenanceTypeID,
                                                                        MyFault.Code,
                                                                        DateTime.Now.ToLongDateString()));
                    }
                    else
                    {
                        LoggerHelper.LogZICDebug(string.Format(@"[非一汽大众车辆初始化]已有合法的保养记录:{0} 上次服务时间 {1}",
                                                                        vmr.MaintenanceTypeID,
                                                                        vmr.LastServiceDate));
                    }
                }
            }


        }

    }
}
